<script>
  import {getCurrentInstance} from 'vue';
  import {getAd, setAd} from "./storage/ad";
  import {setFreeNum} from "./storage/free_num";
  import {getNow} from "./storage/now";
  export default {
    onLaunch: function () {
      const updateManager = uni.getUpdateManager();

      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        // console.log(res.hasUpdate);
      });

      updateManager.onUpdateReady(function (res) {
        uni.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate();
            }
          }
        });
      });

      updateManager.onUpdateFailed(function (res) {
        // 新的版本下载失败
      });

      const {
        proxy
      } = getCurrentInstance()

      proxy.$const.bgMusic = uni.createInnerAudioContext();

      proxy.$const.bgMusic.autoplay = false;//自动播放

      proxy.$const.bgMusic.loop = true;//循环播放

      proxy.$const.bgMusic.src = 'https://seerforwdw.info/ztq.mp3';//背景音乐地址

      proxy.$const.bgMusic.volume = 0.2;//音量

      proxy.$const.bgMusic.onPlay(function () {
        proxy.$const.bgMusic.isStart = true
        console.log('背景音乐播放中');
      });

      proxy.$const.bgMusic.onPause(function () {
        proxy.$const.bgMusic.isStart = false
        console.log('背景暂停');
      });

      this.getOpenId(proxy)
    },
    onShow: function () {
      console.log('App Show')
    },
    onHide: function () {
      console.log('App Hide')
    },
    methods: {
      getOpenId(proxy) {
        if(!uni.getStorageSync('openid')) {
          uni.login({
            provider: 'weixin',
            success: async (res) => {
              let code = res.code
              const {openid} = await proxy.$api.getOpenId({code})
              uni.setStorageSync('openid', openid)
            }
          })
        }
      }
    },
  }
</script>

<style>
  @import '@/uni_modules/tuniaoui-vue3/index.css';
  @import '@/colorui/main.css';
</style>
